package strongholdCraft.common;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import net.minecraft.entity.EnumCreatureType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPotion;
import net.minecraft.potion.Potion;
import net.minecraft.src.ModLoader;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.Property;
import strongholdCraft.common.Items.ArmorItems;
import strongholdCraft.common.Items.Items;
//import strongholdCraft.common.TileEntities.TileEntityInfuser;
import strongholdCraft.common.blocks.Blocks;
import strongholdCraft.common.configuration.ConfigurationHandler;
import strongholdCraft.common.core.CraftingHandler;
import strongholdCraft.common.core.Reference;
import strongholdCraft.common.core.StrongholdCraftEventHooks;
import strongholdCraft.common.core.UpdateHandler;
import strongholdCraft.common.core.VersionCheckTickHandler;
import strongholdCraft.common.entity.EntityCowEnd;
import strongholdCraft.common.infuser.GuiHandler;
import strongholdCraft.common.infuser.TileEntityInfuser;
import strongholdCraft.common.potions.PotionMilk;
import strongholdCraft.common.recipes.Recipes;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.Mod.PostInit;
import cpw.mods.fml.common.Mod.PreInit;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.TickRegistry;
import cpw.mods.fml.relauncher.Side;


@Mod(modid=Reference.MOD_ID, name= Reference.MOD_NAME, version= Reference.VERSION_NUMBER)
@NetworkMod(clientSideRequired=true, serverSideRequired=false)
public class mod_StrongholdCraft {
	//The variables for the config file
	public static boolean doesPumpkinHatShowOverlay;
public GuiHandler guiHandler = new GuiHandler();
	

		// The instance of your mod that Forge uses.
        @Instance("StrongholdCraft")
        public static mod_StrongholdCraft instance;
        
        
        // Says where the client and server 'proxy' code is loaded.
        @SidedProxy(clientSide= Reference.CLIENT_PROXY_CLASS, serverSide=Reference.SERVER_PROXY_CLASS)
        public static CommonProxy proxy;
        
        public static Potion potionMilk;

        @PreInit
        public void preInit(FMLPreInitializationEvent event) {
        	
        	Potion[] potionTypes = null;
        	for(Field f: Potion.class.getDeclaredFields()){
        		f.setAccessible(true);
        		try{
        			if(f.getName().equals("potionTypes") || f.getName().equals("field_76425_a")) {
        				Field modfield = Field.class.getDeclaredField("modifiers");
        				modfield.setAccessible(true);
        				modfield.setInt(f,f.getModifiers() & ~Modifier.FINAL);
        				potionTypes = (Potion[])f.get(null);
        				final Potion[] newPotionTypes = new Potion[256];
        				newPotionTypes[1] = potionMilk;
        				System.arraycopy(potionTypes, 0, newPotionTypes, 0, potionTypes.length);
        				f.set(null, newPotionTypes);
        				
        			}
        		}catch(Exception E){
        			System.err.println(E);
        		}
        	}
        	
        	MinecraftForge.EVENT_BUS.register(new StrongholdCraftEventHooks());
        	
        	
        	
        	
        	 ConfigurationHandler.init(new File(event.getModConfigurationDirectory().getAbsolutePath() + File.separator + Reference.CHANNEL_NAME + File.separator + Reference.MOD_ID + ".cfg"));

             // Conduct the version check and log the result
             UpdateHandler.execute();

             // Initialize the Version Check Tick Handler (Client only)
             TickRegistry.registerTickHandler(new VersionCheckTickHandler(), Side.CLIENT);
        	//Configuration config = new Configuration(event.getSuggestedConfigurationFile());

//            config.load();
//
//           
//
//            // Since this flag is a boolean, we can read it into the variable directly from the config.
//            doesPumpkinHatShowOverlay = config.get(Configuration.CATEGORY_GENERAL, "DoesPumpkinHatShowOverlay", false).getBoolean(false);
//            
//            //Notice there is nothing that gets the value of this property so the expression results in a Property object.
//            //Property someProperty = config.get(Configuration.CATEGORY_GENERAL, "SomeConfigString", "nothing");
//            
//            // Here we add a comment to our new property.
//            //someProperty.comment = "This value can be read as a string!";
//            
//            //String someConfigString = someProperty.value;
//            // this could also be:
//            // int someInt = someProperty.getInt();
//            // boolean someBoolean = someProperty.getBoolean(true);
//
//            config.save();
    }

        
        
        @Init
        public void load(FMLInitializationEvent event) {
        	
        	potionMilk = (new PotionMilk(32, false, 0).setPotionName("PotionMilk"));
        	
        	ModLoader.registerEntityID(EntityCowEnd.class, "EnderCow", 2000);
        	 ModLoader.registerEntityID(EntityCowEnd.class, "EnderCow", 2000, 000255000 , 255000000);
        	 ModLoader.addSpawn(EntityCowEnd.class, 20, 1, 2, EnumCreatureType.monster, new BiomeGenBase[]{BiomeGenBase.sky});
        	 
        	
			NetworkRegistry.instance().registerGuiHandler(this, guiHandler);
        	GameRegistry.registerTileEntity(TileEntityInfuser.class,"tileEntityYourFurnace");
        	
        	

               
                Items.registerNames();
                ArmorItems.addArmor();
                ArmorItems.addArmorNames();
                Recipes.addCraftingRecipes();
                Blocks.RegisterBlocks();
                Blocks.AddNames();
                GameRegistry.registerCraftingHandler(new CraftingHandler());
                
                }

        
        @PostInit
        public void postInit(FMLPostInitializationEvent event) {
                // Stub Method

        }
}

